﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Services;
using Microsoft.Reporting.WinForms;
using MarginTrading.Common;

namespace MarginTrading.Forms
{
    public partial class MarginTradingStatus : Form
    {
        public MarginTradingStatus()
        {
            InitializeComponent();
        }

        private string GetClientCode()
        {
            string clientCode = string.Empty;
            if (!String.IsNullOrEmpty(txtBankAccNo.Text))
            {
                clientCode = ClientService.GetClientCodeBankAccNo(txtBankAccNo.Text);
            }
            else
            {
                if (!String.IsNullOrEmpty(txtClientName.Text))
                {
                    int startIndex = txtClientName.Text.LastIndexOf('(') + 1;
                    int length = txtClientName.Text.LastIndexOf(')') - txtClientName.Text.LastIndexOf('(') - 1;
                    if (length > 0)
                    {
                        clientCode = txtClientName.Text.Substring(startIndex, length);
                    }
                }
                else
                {
                    string strClientCode = "{0}-{1}-{2}-{3}";
                    clientCode = string.Format(strClientCode, txtClientIDPrefix.Text, txtClientID1.Text, txtClientID2.Text,
                        txtClientID3.Text);
                }
            }
            if (!checkBox1.Checked)
            {
                return "All";
            }

            return clientCode;
        }

        private void LoadReport(string clientCode, string clientCategory)
        {
            try
            {
                //Advisor advisor = AdvisorServices.GetAdvisorByCode(advisorId);

                //if (advisor == null)
                //{
                //    //advisor = AdvisorServices.GetAdvisorByCode(advisorId.ToUpper());
                //    //if (advisor == null)
                //    //{
                //        advisor = new Advisor();
                //        advisor.Code = "All";
                //    //}
                //    //else
                //    //{
                //    //    advisorId = advisorId.ToUpper();
                //    //}
                //}

                Client client = ClientService.GetClientByCode(clientCode);
                if (client == null)
                {
                    client = new Client();
                }
                if (clientCode == "All")
                {
                    client.BankAccNo = "All";
                    client.Surname = "All";
                    client.Code = "All";
                }

                ReportDataSource ds = new ReportDataSource();
                ds.Name = "master";

                //if (clientCode == "All")
                //{
                //    ds.Value = ReportServices.GetMarginTradingStatusByAdvisor();
                //}

                ds.Value = ReportServices.GetMarginTradingStatus(clientCode, clientCategory);

                this.reportViewer1.LocalReport.ReportPath = "MarginTradingStatus.rdlc";
                this.reportViewer1.LocalReport.DataSources.Clear();
                this.reportViewer1.LocalReport.DataSources.Add(ds);

                Config AgentCodeConfig = ConfigServices.GetConfigByName("Agent Code");
                string AgentCode = "cts";
                if (AgentCodeConfig != null)
                {
                    AgentCode = AgentCodeConfig.Value;
                }

                //TODO:Remove this 
                AgentCode = Reports.AGENT_CODE;

                this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] {
                        new ReportParameter("ImageLink",AgentCode.ToLower()),
                        new ReportParameter("CompanyName",Reports.COMPANY_NAME),
                        new ReportParameter("BankAccNo",client.BankAccNo),
                        new ReportParameter("ClientID",client.Code),
                        new ReportParameter("ClientCategory",ddlClientCategory.Text),
                        new ReportParameter("ClientName",string.Concat(client.Initials, 
                                                            " ", client.Surname) ),
                        new ReportParameter("ReportTitle",Reports.MARGIN_TRADING_TITLE)});

                this.reportViewer1.RefreshReport();
                reportViewer1.SetDisplayMode(DisplayMode.PrintLayout);
                reportViewer1.ZoomMode = ZoomMode.Percent;
                reportViewer1.ZoomPercent = 100;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToEventLog(e.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            if (!String.IsNullOrEmpty(GetClientCode()) && GetClientCode() != "---")
            {
                LoadReport(GetClientCode(),ddlClientCategory.SelectedValue.ToString());
            }
            button1.Enabled = true;
        }

        private void lblClientID_Click(object sender, EventArgs e)
        {
            
        }

        private void MarginTradingStatus_Load(object sender, EventArgs e)
        {
            DataTable clientCategory = ClientCategoryServices.GetClientCategoryCollectionDataTable();
            DataRow dr = clientCategory.NewRow();
            dr["CODE"] = "All";
            dr["DESCRIPTION"] = "All";
            clientCategory.Rows.InsertAt(dr, 0);
            ddlClientCategory.DisplayMember = "DESCRIPTION";
            ddlClientCategory.ValueMember = "CODE";
            ddlClientCategory.DataSource = clientCategory;
            checkBox1.Checked = true;


            AutoCompleteStringCollection clientNames = ClientService.GetClientNameCollection();
            AutoCompleteStringCollection bankAccNo = ClientService.GetBankAccNoCollection();
            if (clientNames != null)
            {
                txtClientName.AutoCompleteCustomSource = clientNames;
            }
            if (bankAccNo != null)
            {
                txtBankAccNo.AutoCompleteCustomSource = bankAccNo;
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                label2.Enabled = true;
                label3.Enabled = true;
                label1.Enabled = true;
                txtBankAccNo.Enabled = true;
                txtClientName.Enabled = true;
                txtClientIDPrefix.Enabled = true;
                txtClientID1.Enabled = true;
                txtClientID2.Enabled = true;
                txtClientID3.Enabled = true;
                ddlClientCategory.Enabled = false;
            }
            else
            {
                label2.Enabled = false;
                label3.Enabled = false;
                label1.Enabled = false;
                txtBankAccNo.Enabled = false;
                txtClientName.Enabled = false;
                txtClientIDPrefix.Enabled = false;
                txtClientID1.Enabled = false;
                txtClientID2.Enabled = false;
                txtClientID3.Enabled = false;
                ddlClientCategory.Enabled = true;
            }
        }
    }
}
